package com.hotel.ordersystem.service.impl;

import com.common.status.ResultMsg;
import com.hotel.config.rabbitmq.config.CommentReplyConfig;
import com.hotel.config.rabbitmq.config.PersonalCouponWebSocketDirectConfig;
import com.hotel.config.rabbitmq.config.RedisUnReadWebSocketConfig;
import com.hotel.customersystem.pojo.CustomerUser;
import com.hotel.customersystem.service.CustomerUserService;
import com.hotel.ordersystem.pojo.CommentMongoDB;
import com.hotel.ordersystem.pojo.CommentReplyMongoDB;
import com.hotel.ordersystem.service.CommentReplyService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CommentReplyServiceImpl implements CommentReplyService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private CustomerUserService customerUserService;

    @Override
    public String saveCommentReply(CommentReplyMongoDB commentReplyMongoDB) {
        mongoTemplate.save(commentReplyMongoDB);
        return ResultMsg.SUCCESS_MSG;
    }

    @Override
    public List<CommentReplyMongoDB> getCommentReplyBig(String commentId) {
        Query query = new Query(Criteria.where("user1commentReplyId").is(commentId));
        List<CommentReplyMongoDB> commentReplyMongoDBS = mongoTemplate.find(query, CommentReplyMongoDB.class);
        return commentReplyMongoDBS;
    }

    @Override
    public Boolean setCommentReplyInformation() {
        List<CommentReplyMongoDB> commentReplyMongoDBList = mongoTemplate.findAll(CommentReplyMongoDB.class);
        for (CommentReplyMongoDB commentReplyMongoDB : commentReplyMongoDBList) {
            //1.查询被回复的用户名是否被修改
            CustomerUser customerUser1 = customerUserService.getById(commentReplyMongoDB.getUser1());
            //2.查询回复的用户名以及用户头像是否被修改
            CustomerUser customerUser2 = customerUserService.getById(commentReplyMongoDB.getUser2());
            //3.更新mongoDB数据
            Query query = new Query(Criteria.where("commentReplyId").is(commentReplyMongoDB.getCommentReplyId()));
            Update update = new Update();
            update.set("user1UserName",customerUser1.getUserNickName());
            update.set("user2UserName",customerUser2.getUserNickName());
            update.set("user2Photo",customerUser2.getStandby02());
            mongoTemplate.updateFirst(query,update,CommentReplyMongoDB.class);
            setCommentReplyInformation1(commentReplyMongoDB.getCommentReplyId());
            return true;
        }
        return null;
    }

    @Override
    public Boolean setCommentReplyInformation1(String commentReplyId) {
        Query query = new Query(Criteria.where("user1commentReplyId").is(commentReplyId));
        List<CommentReplyMongoDB> commentReplyMongoDBList = mongoTemplate.find(query,CommentReplyMongoDB.class);
        for (CommentReplyMongoDB commentReplyMongoDB : commentReplyMongoDBList) {
            //1.查询被回复的用户名是否被修改
            CustomerUser customerUser1 = customerUserService.getById(commentReplyMongoDB.getUser1());
            //2.查询回复的用户名以及用户头像是否被修改
            CustomerUser customerUser2 = customerUserService.getById(commentReplyMongoDB.getUser2());
            //3.更新mongoDB数据
            Query query1 = new Query(Criteria.where("commentReplyId").is(commentReplyMongoDB.getCommentReplyId()));
            Update update = new Update();
            update.set("user1UserName",customerUser1.getUserNickName());
            update.set("user2UserName",customerUser2.getUserNickName());
            update.set("user2Photo",customerUser2.getStandby02());
            mongoTemplate.updateFirst(query,update,CommentReplyMongoDB.class);
        }
        return null;
    }

    /**
     *
     * @param user1 接收方
     * @param user2 发送方
     */
    @Override
    public void saveRabbitCommentReply(String user1, String user2,String text) {
        CustomerUser customerUser = customerUserService.getById(user2);
//        String userId = text+"-"+user1 + "-"+user2;
        String userId = customerUser.getUserNickName() + "评论了你:"+text + "-" + user1;
        MessageProperties messageProperties = new MessageProperties();
        Message message = new Message(userId.getBytes(),messageProperties);
        //将消息存放到消息队列当中
        System.out.println("消息已经存-->消息队列...");
        rabbitTemplate.convertAndSend(CommentReplyConfig.COMMENT_REPLY_NORMAL,
               CommentReplyConfig.COMMENT_REPLY_KEY,message);
    }
}
